package game;

import java.awt.Color;

public class Platform extends GameRectangle {
	public static final long serialVersionUID = 1L;
	
	public Platform(){}
	
	public Platform(Level g, int x ,int y ,int w ,int h, Color c){
        super(g, x ,y , w, h, c);
        prevX = x;
        prevY = y;
        xMotion = 0;
        downMotion = 0;
    }
	
	@Override
	public void calculateIntersectEffects(GameRectangle g){
		//if a sprite (like mario)
		if(g instanceof Sprite){
			Sprite s = (Sprite)g;
			//create vectors for the four points
    		Vector bottomRight = new Vector(s.prevX+s.curWidth+xMotion(), s.prevY+s.curHeight+downMotion(), s.x+s.curWidth+s.xMotion(), s.y+s.curHeight+s.downMotion());
    		Vector topLeft = new Vector(s.prevX+xMotion(), s.prevY+downMotion(), s.x+s.xMotion(), s.y+s.downMotion());
    		Vector bottomLeft = new Vector(s.prevX+xMotion(), s.prevY+s.curHeight+downMotion(), s.x+s.xMotion(), s.y+s.curHeight+s.downMotion());
    		float[] dirs = {100, 100, 100, 100};
    		dirs[0] = bottomRight.distanceToX(x);
    		dirs[1] = topLeft.distanceToX(x+width);
    		dirs[2] = bottomRight.distanceToY(y);
    		dirs[3] = topLeft.distanceToY(y+height);
    		int index = -1;
    		float min = 1.1f;
    		for(int i = 0; i < dirs.length; i++){
    			if(dirs[i] <= min && dirs[i] >= -.1){
    				min = dirs[i];
    				index = i;
    			}
    		}
    		
    		switch(index){
    		case 0:
    			if(bottomRight.calculateYatX(x) <= y+15){
    				s.bottomHit(this);
    				topHit(s);
    			}else{
    				s.rightHit(this);
    				leftHit(s);
    			}
    			break;
    		case 1:
    			if(bottomLeft.calculateYatX(x+width) <= y+15){
    				s.bottomHit(this);
    				topHit(s);
    			}else{
    				s.leftHit(this);
    				rightHit(s);
    			}
    			break;
    		case 2:
    			s.bottomHit(this);
    			topHit(s);
    			break;
    		case 3:
    			s.topHit(this);
    			bottomHit(s);
    			break;
    		default:
    				break;
    		}
    		
		}
    }
	
	public int calculateXMovement(Sprite s){
		return (int)s.xMotion - (int)xMotion;
	}
	
	public int calculateYMovement(Sprite s){
		return (int)s.downMotion - (int)downMotion;
	}
	
	public float calcTimeOfIntersect(Vector a, Vector b){
		int temp = (a.x2-a.x1)*(a.x1) - (b.x2-b.x1)*(b.x1) + a.y1 - b.y1;
		float temp2 = (float)temp/((a.x2-a.x1) - (b.x2-b.x1)+.000001f);
		return (float)(temp2-a.x2)/(a.x1-a.x2);
	}
	
	/*
	public void draw(){
		level.canvas.bufGraphics.setColor(color.darker());
		level.canvas.bufGraphics.fillRect(x-level.canvas.currentX+20, y-level.canvas.currentY+20, width, height);
		level.canvas.bufGraphics.setColor(color.darker().darker());
		int[] x1 = {x-level.canvas.currentX-20, x+width-level.canvas.currentX-20, x+width-level.canvas.currentX+20, x-level.canvas.currentX+20};
		int[] y1 = {y-level.canvas.currentY-20, y-level.canvas.currentY-20, y-level.canvas.currentY+20, y-level.canvas.currentY+20};
		level.canvas.bufGraphics.fillPolygon(x1, y1, 4);
		level.canvas.bufGraphics.setColor(color.brighter());
		int[] x2 = {x-level.canvas.currentX-20, x-level.canvas.currentX-20, x-level.canvas.currentX+20, x-level.canvas.currentX+20};
		int[] y2 = {y-level.canvas.currentY-20, y+height-level.canvas.currentY-20, y+height-level.canvas.currentY+20, y-level.canvas.currentY+20};
		level.canvas.bufGraphics.fillPolygon(x2, y2, 4);
		level.canvas.bufGraphics.setColor(Color.black);
		level.canvas.bufGraphics.drawRect(x-level.canvas.currentX, y-level.canvas.currentY, width, height);
	}
	*/
	
	@Override
	public void update(){
		super.update();
	}
	
	public void applyPhysics(Sprite s){}
	
	public <E> void out(Object o){
		System.out.println(o.toString());
	}
}
